<template>
  <a-modal
    :title="'修改商户 ' + name"
    :visible="visible"
    @ok="onSubmit"
    @cancel="visible = false"
  >
    <a-form-model
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 14 }"
    >
      <a-form-model-item ref="top_up" label="加减币" prop="top_up">
        <a-input-number
          v-model="form.top_up"
          :formatter="(value) => (value > 0 ? '+' + value : value)"
          :min="-origin_coins"
          @blur="
            () => {
              $refs.top_up.onFieldBlur()
            }
          "
        />
        <span class="ml-2"> 余额: {{ origin_coins }}</span>
      </a-form-model-item>

      <a-form-model-item ref="fee_ratio" label="卖币手续费率" prop="fee_ratio">
        <a-input-number
          v-model="form.fee_ratio"
          :step="0.001"
          @blur="
            () => {
              $refs.fee_ratio.onFieldBlur()
            }
          "
        />
      </a-form-model-item>

      <a-form-model-item ref="tip" label="下发手续费" prop="tip">
        <a-input-number
          v-model="form.tip"
          @blur="
            () => {
              $refs.tip.onFieldBlur()
            }
          "
        />
      </a-form-model-item>

      <a-form-model-item label="登录密码" prop="password">
        <a-input v-model="form.password" />
      </a-form-model-item>

      <a-form-model-item label="启用/禁用" prop="enable">
        <a-switch v-model="form.enable" />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      visible: false,
      id: null,
      name: '',
      origin_coins: 0,
      form: {
        password: '',
        top_up: 0,
        fee_ratio: 0,
        tip: 0,
        enable: false,
      },
      rules: {
        top_up: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
        fee_ratio: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
        tip: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
        password: [
          {
            min: 6,
            message: '长度必须在6位以上 ',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  computed: {
    ...mapGetters('account', ['sys']),
  },
  created() {
    // console.log(this.sys)
  },
  methods: {
    show(row) {
      this.origin_coins = row.coins
      this.id = row.id
      this.name = row.user.username
      this.form = {
        password: '',
        top_up: 0,
        fee_ratio: row.fee_ratio,
        tip: row.tip,
        enable: row.enable,
      }
      this.visible = true
    },
    onSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //   console.log(this.form)
          this.axios.put(`/sellers/${this.id}`, this.form).then((res) => {
            this.visible = false
            this.$message.success('修改成功')
            this.$emit('refresh')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>
